package com.grupo26.mremployee;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LVAdapter extends BaseAdapter {

	private Context ctx;
	private ArrayList<Pedido> list;
	private int numero_pedidos;
	
	public LVAdapter(Context ctx, ArrayList<Pedido> pdd, int numero_pedidos){
		this.list = pdd;
		this.ctx = ctx;
		this.numero_pedidos = numero_pedidos;
	}
	@Override
	public int getCount() {
		return list.get(numero_pedidos).getArraySize();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return list.get(position).getCod();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		
		View itemView;
		LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		  
		if (convertView == null){
		    
			itemView = new View(ctx);
		    
		    itemView = inflater.inflate(R.layout.item_view, null);
		    TextView textNome = (TextView) itemView.findViewById(R.id.item_nome);
		    TextView textQtd = (TextView) itemView.findViewById(R.id.item_quantidade);
		    ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
			   
	    	textNome.setText(list.get(numero_pedidos).getSpecificProductName(position));
		    textQtd.setText(Integer.toString(list.get(numero_pedidos).getQuantidadeProduto(position)));
		    imageView.setOnClickListener(new OnClickListener() {
	
			    	@Override
		            public void onClick(View v) {
			    		if(ctx instanceof MainActivity){
			    			((MainActivity)ctx).itemRemoveClickedList(position);
			    		}
		            }
		        });
		    
			} else {
				itemView = (View) convertView;
		}

		return itemView;
	}
}
